from rest_framework.response import Response
from rest_framework.decorators import api_view, parser_classes
from rest_framework.parsers import FormParser
from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi

# Create your views here.

# 【FBV演示】GET请求，通过URL query获取参数
@swagger_auto_schema(
    tags=["01:基于FBV的GET/POST请求"],
    method="get",
    operation_summary="通过URL传递参数获取GET请求数据",
    manual_parameters=[
        openapi.Parameter(
            "q",
            openapi.IN_QUERY,
            description="查询参数q",
            type=openapi.TYPE_STRING,
            required=True,
        ),
        openapi.Parameter(
            "num",
            openapi.IN_QUERY,
            description="查询参数num",
            type=openapi.TYPE_INTEGER,
            required=True,
        ),
    ],
)

@api_view(["GET"])
def get_query(request):
    q = request.query_params.get("q")
    num = request.query_params.get("num")
    # return HttpResponse(f"GET请求：通过URL query获取到q={q}, num={num}")
    return Response(
        {
            "code": 0,
            "msg": "GET query:success",
            "data":{
                "q": q,
                "num": num,
            },
        }
    )

# 【FBV演示】GET请求，通过URL path获取参数
@swagger_auto_schema(
    tags=["01:基于FBV的GET/POST请求"],
    method="get",
    operation_summary="通过URL/path方式传递参数获取GET请求数据",
    manual_parameters=[
        openapi.Parameter(
            "q",
            openapi.IN_PATH,
            description="查询参数q",
            type=openapi.TYPE_STRING,
            required=True,
        ),
        openapi.Parameter(
            "num",
            openapi.IN_PATH,
            description="查询参数num",
            type=openapi.TYPE_INTEGER,
            required=True,
        ),
    ],
)

@api_view(["GET"])
def get_path(request, q, num):
    return Response(
        {
            "code": 0,
            "msg": "GET path:success",
            "data":{
                "q": q,
                "num": num,
            },

        }
    )

# 【FBV演示】POST请求，接收formdata数据
@swagger_auto_schema(
    tags=["01:基于FBV的GET/POST请求"],
    method="POST",
    operation_summary="接收formdata格式的POST请求数据",
    manual_parameters=[
        openapi.Parameter(
            "q",
            openapi.IN_FORM,
            description="查询参数q",
            type=openapi.TYPE_STRING,
            required=True,
        ),
        openapi.Parameter(
            "num",
            openapi.IN_FORM,
            description="查询参数num",
            type=openapi.TYPE_INTEGER,
            required=True,
        ),
    ]
)

@api_view(["POST"])
@parser_classes([FormParser])
def post_formdata(request):
    q = request.data.get("q", "default")
    num = request.data.get("num")
    return Response(
        {
            "code": 0,
            "msg": "POST formdata:success",
            "data":{
                "q": q,
                "num": num,
            },
        }
    )

# 【FBV演示】GET请求，接收JSON数据
@swagger_auto_schema(
    tags=["01:基于FBV的GET/POST请求"],
    method="post",
    operation_summary="接收JSON格式的POST请求数据",
    request_body=openapi.Schema(
        type=openapi.TYPE_OBJECT,
        properties={
            "q": openapi.Schema(
                type=openapi.TYPE_STRING, description="提交参数q"
            ),
            "num":openapi.Schema(
                type=openapi.TYPE_INTEGER, description="提交参数num"
            ),
        },
        required=["q", "num"]
    ),
)

@api_view(["POST"])
def post_json(request):
    q = request.data.get("q", "default")
    num = request.data.get("num")
    return Response( 
        {
           "code": 0,
           "msg": "POST json:success",
           "data":{
               "q": q,
               "num": num,
           } ,
        }
    )
    